package ccnt.pvc.scudwaremobile.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.util.Log;
import ccnt.pvc.scudwaremobile.bean.ResultObject;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
 * 封装HttpClient的Get和Post方法，返回ObjectResult实例
 * code表示代码，result是结果，result需要json解析
 * @author dy
 *
 */
public class HttpClientUtils {
	
	private static final String ENCODING = "UTF-8";
	
	public static ResultObject sendGet(String url, Map<String, Object> params) {
		return sendGet(url, ENCODING, params);
	}
	
	public static ResultObject sendPost(String url,List<NameValuePair> params) {
		ResultObject resultObject = new ResultObject();
		HttpClient httpClient = new DefaultHttpClient();
		try{
			HttpPost method = new HttpPost(url);
			method.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response = httpClient.execute(method);
			String content = EntityUtils.toString(response.getEntity(),ENCODING);
			JsonObject object = (JsonObject)(new JsonParser().parse(content));
			resultObject.setCode(object.get("code").getAsInt());
			resultObject.setResult(object.get("result").toString());
		}catch(Exception e) {
			e.printStackTrace();
		}finally{
			httpClient.getConnectionManager().shutdown();
		}
		return resultObject;
	}

	public static ResultObject sendGet(String url, String encoding,
			Map<String, Object> params) {
		ResultObject resultObject = new ResultObject();
		HttpClient httpClient = new DefaultHttpClient();
		try {
			url = assambleUrl(url, encoding, params);
			HttpGet method = new HttpGet(url);
			HttpResponse response = httpClient.execute(method);
			String content = EntityUtils.toString(response.getEntity(),
					ENCODING);
			Log.d("content", content);
			JsonObject object = (JsonObject)(new JsonParser().parse(content));
			resultObject.setCode(object.get("code").getAsInt());
			resultObject.setResult(object.get("result").toString());
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			httpClient.getConnectionManager().shutdown();
		}
		return resultObject;
	}

	private static String assambleUrl(String baseUrl, String encoding,
			Map<String, Object> params) {
		if (params == null || params.isEmpty()) {
			return baseUrl;
		}
		StringBuilder sb = new StringBuilder(baseUrl);
		if (!baseUrl.endsWith("?") && !baseUrl.contains("&")) {
			sb.append("?");
		} else if (baseUrl.contains("&")) {
			sb.append("&");
		}
		for (String key : params.keySet()) {
			try {
				sb.append(key)
						.append("=")
						.append(URLEncoder.encode(
								String.valueOf(params.get(key)), encoding))
						.append("&");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return sb.deleteCharAt(sb.length() - 1).toString();
	}
	
}
